<template>
	<view class="page-container">
		<!-- 吸顶部分 -->
		<u-sticky bgColor="rgba(64, 21, 5, 1)">
			<view class="header-top">
				<view class="header-top-left">
					<u-icon name="arrow-left" color="#fff" size="15" @click="onBack()"></u-icon>
				</view>
				<view class="header-top-center">
					徽章成就
				</view>
				<view class="header-top-right">
					<!-- <image src="/static/index/checkin/checkinNavBarIcon.png" mode=""></image> -->
				</view>
			</view>
		</u-sticky>

		<!-- 用户信息 -->
		<view class="userInfo">
			<view class="userInfo-topLeft">
				<view class="userInfo-avatar">
					<image src="/static/user-badge/avatar.png" mode=""></image>
				</view>
				<view class="userInfo-userName">
					茶杏丸丸
				</view>
			</view>
			<view class="userInfo-topRight">
				<image src="/static/user-badge/badgeSum.png" mode=""></image>
			</view>
		</view>

		<zvMbanner style="min-height: 380rpx;" :items="badgeList" :select="currentBannerIndex"
			@change="handleBannerChange" :key="`banner-${currentIndex}`">
		</zvMbanner>

		<!-- 标签详细信息 -->
		<view class="badgeDetail" v-for="(item,index) in badgeList" :key="index">
			<view class="badgeDetail-top" v-show="index == currentBannerIndex">
				{{item.title}}
			</view>
			<view class="badgeDetail-bottom" v-show="index == currentBannerIndex">
				{{item.description}}
			</view>
		</view>

		<!-- 标签页 -->
		<view class="tabContainer">
			<!-- 下划线指示器 -->
			<view class="tabIndicator" :style="{ transform: `translateX(${currentIndex * 100}%)` }"></view>

			<view class="tabItem" v-for="(item, index) in tabsList" :key="index" @click="handleTabClick(index)"
				:class="{ active: currentIndex === index }">
				{{ item.name }}
			</view>
		</view>

		<!-- 内容区域 -->
		<view class="content">
			<view class="badge-list">
				<view class="badge-item" v-for="(badge, index) in badgeList" :key="index"
					@click="handleBadgeClick(index)">
					<view class="badge-item-img">
						<image :src="badge.imgUrl" mode=""></image>
					</view>
					<view class="badge-item-title">
						{{ badge.title }}
					</view>
					<view class="badge-item-status">
						<view class="status-tag"
							:class="{ 'active': badge.status === 1, 'inactive': badge.status === 0 }">
							{{ badge.status === 1 ? '已点亮' : '未点亮' }}
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import zvMbanner from "@/components/zv-mbanner/zv-mbanner.vue"

	export default {
		components: {
			zvMbanner
		},
		data() {
			return {
				tabsList: [{
						name: '全部'
					},
					{
						name: '本地徽章'
					},
					{
						name: '避坑徽章'
					}
				],
				// 避坑图标
				avoidPitfallsList: [{
						title: '避坑新人',
						imgUrl: '/static/user-badge/AvoidingPitfallsNewcomer.png',
						status: 1, //已点亮1，未点亮0
						description: '发布第1条避坑帖'

					},
					{
						title: '避坑先锋',
						imgUrl: '/static/user-badge/AvoidingPitfallsPioneer.png',
						status: 1, //已点亮1，未点亮0
						description: '累计发布5条避坑帖，图文并茂、描述清晰、获得至少10个点赞'
					},
					{
						title: '避坑达人',
						imgUrl: '/static/user-badge/AvoidingPitfallsExpert.png',
						status: 1, //已点亮1，未点亮0
						description: '累计发布20条避坑帖，≥5条获得500次浏览/100个点赞/50条收藏'
					},
					{
						title: '避坑专家',
						imgUrl: '/static/user-badge/AvoidPitfallsProfessional.png',
						status: 1, //已点亮1，未点亮0
						description: '累计发布50条避坑帖，≥5条获得5000次浏览/500个点赞/200条收藏'
					},
				],
				localList: [{
						title: '胡同串子',
						imgUrl: '/static/user-badge/basicIndigenous.png',
						status: 1, //已点亮1，未点亮0
						description: '成功发布第1篇推荐帖'
					},
					{
						title: '坊间掌柜',
						imgUrl: '/static/user-badge/intermediateIndigenous.png',
						status: 1, //已点亮1，未点亮0
						description: '拥有至少5篇“推荐帖”（单篇浏览量>200或点赞>50）'
					},
					{
						title: '御前带路官',
						imgUrl: '/static/user-badge/advancedIndigenous.png',
						status: 1, //已点亮1，未点亮0
						description: '拥有至少10篇“爆款推荐”（单篇浏览量>2000或点赞>200）'
					},
					{
						title: '逍遥城主',
						imgUrl: '/static/user-badge/SpecialGradeIndigenous.png',
						status: 0, //已点亮1，未点亮0
						description: '拥有至少20篇“爆款推荐”（单篇浏览量>5000或点赞>500）'
					}
				],
				otherList: [{
					title: '美食雷达',
					imgUrl: '/static/user-badge/FoodRadar.png',
					status: 1, //已点亮1，未点亮0
					description: '累计发布10条美食相关贴'
				}],
				badgeList: [],
				currentIndex: 0, // 当前选中的标签页索引
				currentBannerIndex: 0, // 当前轮播图选中的索引
			};
		},
		watch: {
			// 监听 currentIndex 变化，更新 badgeList
			currentIndex(newVal) {
				this.updateBadgeList();
			},
			// 监听 badgeList 变化，重置轮播图索引
			badgeList() {
				this.currentBannerIndex = 0;
			}
		},
		methods: {
			onBack() {
				uni.navigateBack();
			},
			handleTabClick(index) {
				this.currentIndex = index;
				this.updateBadgeList();
			},
			updateBadgeList() {
				// 根据当前标签页更新 badgeList
				if (this.currentIndex === 0) {
					// 全部：合并 avoidPitfallsList、otherList、localList
					this.badgeList = [...this.localList, ...this.otherList, ...this.avoidPitfallsList];
				} else if (this.currentIndex === 1) {
					// 本地徽章：只显示 localList
					this.badgeList = this.localList;
				} else if (this.currentIndex === 2) {
					// 避坑徽章：只显示 avoidPitfallsList
					this.badgeList = this.avoidPitfallsList;
				}
			},
			// 处理徽章点击事件
			handleBadgeClick(index) {
				console.log(index);
				this.currentBannerIndex = index;
			},
			// 处理轮播图变化事件
			handleBannerChange(index) {
				this.currentBannerIndex = index;
			}
		},
		created() {
			// 页面创建时初始化 badgeList
			this.updateBadgeList();
		}
	};
</script>

<style lang="scss" scoped>
	.page-container {
		background-color: rgba(64, 21, 5, 1);
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		-webkit-overflow-scrolling: touch;

		.header-top {
			padding: 84rpx 30rpx 0;
			height: 72rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: transparent;

			.header-top-left {
				height: 56rpx;
				display: flex;
				align-items: center;

				u-icon {
					color: #fff;
					size: 15;
				}
			}

			.header-top-center {
				font-size: 32rpx;
				font-weight: 700;
				color: #fff;
			}

			.header-top-right {
				image {
					width: 74rpx;
					height: 62rpx;
				}
			}
		}

		.userInfo {
			display: flex;
			justify-content: space-between;
			padding: 40rpx 30rpx;
			height: 80rpx;
			line-height: 80rpx;

			.userInfo-topLeft {
				display: flex;

				.userInfo-avatar {
					width: 80rpx;
					height: 80rpx;

					image {
						width: 80rpx;
						height: 80rpx;
					}
				}

				.userInfo-userName {
					margin-left: 20rpx;
					font-weight: 600;
					font-size: 30rpx;
					color: #fff;
				}
			}

			.userInfo-topRight {
				width: 160rpx;
				height: 80rpx;

				image {
					width: 164rpx;
					height: 80rpx;
				}
			}
		}

		.center-swiper {
			width: 100%;
			height: 288rpx;

			image {
				width: 100%;
				height: 288rpx;
			}
		}

		.badgeDetail {
			text-align: center;
			margin: 0 auto;
			width: 60%;

			.badgeDetail-top {
				font-size: 36rpx;
				font-weight: 700;
				color: rgba(255, 255, 255, 1);
				margin-bottom: 10rpx;
				line-height: 40rpx;
			}

			.badgeDetail-bottom {
				font-size: 26rpx;
				font-weight: 500;
				color: #e1e1e1;
				line-height: 40rpx;
				/* 固定高度确保两行显示 */
				height: 80rpx;
				/* 文本溢出处理 */
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
				text-overflow: ellipsis;
				/* 文本居中 */
				display: flex;
				align-items: center;
				justify-content: center;
				text-align: center;
				/* 添加内边距 */
				padding: 0 20rpx;
				margin-bottom: 20rpx;
			}
		}

		.tabContainer {
			display: flex;
			justify-content: space-between;
			background-color: rgba(64, 21, 5, 1);
			border-bottom: 2rpx solid rgba(120, 120, 130, 0.3);
			padding: 20rpx 0;
			position: relative;

			.tabItem {
				flex: 1;
				/* 每个 tab 项占据相等宽度 */
				text-align: center;
				/* 文字水平居中 */
				font-size: 28rpx;
				font-weight: 500;
				color: rgba(139, 139, 140, 1);
				padding: 10rpx 0;
				transition: all 0.3s ease;
				text-align: center;

				&.active {
					color: rgba(255, 255, 255, 1);
					font-weight: 900;
				}
			}

			// 下划线指示器
			.tabIndicator {
				position: absolute;
				bottom: -3rpx;
				left: 0;
				width: calc(100% / 3);
				height: 6rpx;
				background-color: rgba(251, 169, 44, 1);
				border-radius: 2rpx;
				transition: transform 0.3s ease;
			}
		}

		// 修改 content 部分的样式
		.content {
			flex: 1;
			padding: 30rpx;

			.badge-list {
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between; // 使用 space-between 实现自适应间距

				.badge-item {
					width: 180rpx; // 固定宽度
					height: 278rpx;
					text-align: center;
					margin-bottom: 20rpx; // 添加底部间距

					.badge-item-img {
						width: 180rpx;
						height: 180rpx;
						margin: 0 auto;

						image {
							width: 180rpx;
							height: 180rpx;
						}
					}

					.badge-item-title {
						margin-top: 10rpx;
						color: #fff;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
						font-size: 30rpx;
						font-weight: 500;
					}

					.badge-item-status {
						margin-top: 10rpx;

						.status-tag {
							font-size: 24rpx;
							border-radius: 20rpx;
							color: #fff;
							font-weight: 500;
							width: 110rpx;
							height: 50rpx;
							line-height: 50rpx;
							margin-left: 35rpx;
						}

						.active {
							background-color: rgba(246, 202, 77, 1);
						}

						.inactive {
							background-color: rgba(217, 217, 217, 1);
							color: rgba(0, 0, 0, 1);
						}
					}
				}
			}
		}

	}
</style>